var editFlag = undefined;//设置一个编辑标记
function formonload(){
    //经销商编码不可以编辑
    $('#cCusCode').attr("disabled","disabled");
    //原生datagrid
    var currentNodeid = $("#WF_CurrentNodeid").val();
    $('#controlDiv').show();
    var uid=$("#WF_DocUnid").val();
    $('#dc').datagrid({
        title:"授权客户+产品列表" ,
        singleSelect:false,
       // collapsible:true,
        method:'get',
        autoRowHeight:true,
        fitColumns:true,
        rownumbers:true,
        nowrap:false,
        //idField:'WF_OrUnid',
        url:'rule?wf_num=R_CRMVR_B126&DocUnid='+uid,
        queryParams:{
           
        },
        columns:[[
            {title:'序号',field:'icon',width:30,sortable:false,checkbox:($("#WF_ReadOnly").val()!='true')?true:false},
            {title:'主键',field:'WF_OrUnid',hidden:true,sortable:false,},
            {title:'授权编号',field:'Define4',width:100,sortable:false,
                editor:{
                    type:'text',
                    options:{
                        required: true,
                        width:50
                    }
                }
            },
            {title:'客户编码',field:'Define1',width:100,sortable:false,},
            {title:'授权客户（新客户请先做新终端引入申请）',field:'cInvCode',width:100,sortable:false,
        
            },
            {title:'产品及产品线',field:'cInvName',width:100,sortable:false,
                editor:{
                    type:'text',
                    options:{
                        required: true,
                        width:50
                    }
                }
            },
            {title:'授权有效期',field:'OrderNo',width:100,sortable:false,
                editor:{
                    type:'text',
                    options:{
                        required: true,
                        width:50
                    }
                }
            },
            {title:'授权类型',field:'Define3',width:100,sortable:false,
                editor:{
                    type:'combobox',
                    options:{
                        valueField:'productid',
                        textField:'name',
                        data:[{productid:'入院备案',name:'入院备案'},{productid:'投标',name:'投标'},{productid:'短期推广',name:'短期推广'},{productid:'收款',name:'收款'},{productid:'特殊招标',name:'特殊招标'},{productid:'其他授权',name:'其他授权'}],
                        required:true
                    }
                }
            },
            // {title:'经销商名称',field:'Hosptal',width:100,sortable:false,
            // },
            {title:'经直销',field:'Define2',width:100,sortable:false,
            },
        ]],
            onAfterEdit: function (rowIndex, rowData, changes) {//在添加完毕endEdit，保存时触发
                editFlag = undefined;//重置
            },
            onDblClickCell: function (rowIndex, field, value) {//双击该行修改内容
                //alert("双击行修改数据");
                field.editing = true;
                if (editFlag != undefined) {
                    $("#dc").datagrid('endEdit', editFlag);//结束编辑，传入之前编辑的行
                }
                if (editFlag == undefined) {
                    $("#dc").datagrid('beginEdit', rowIndex);//开启编辑并传入要编辑的行
                    editFlag = rowIndex;
                    if(currentNodeid!="T10002"){
                        $('#dc').datagrid('getEditor', { index: rowIndex, field: 'OrderNo'}).target.prop('disabled',true);
                        $('#dc').datagrid('getEditor', { index: rowIndex, field: 'cInvName'}).target.prop('disabled',true);
                        $('#dc').datagrid('getEditor', { index: rowIndex, field: 'Define3'}).target.combobox('disable');
                    }
                    if(currentNodeid!="T10014"&¤tNodeid!="T10038"){
                        $('#dc').datagrid('getEditor', { index: rowIndex, field: 'Define4'}).target.prop('disabled',true);
                    }
                }
            }
            
    });
    
    var WF_IsFirstNodeFlag = $("#WF_IsFirstNodeFlag").val();
    if(currentNodeid=="T10002"&&WF_IsFirstNodeFlag=="true"){
      $('#dc').datagrid({
                toolbar: [{//在dategrid表单的头部添加按钮
                text: "新增",
                iconCls: "icon-add",
                handler: function () {
                    SelInventory();
                }
              
            }, '-', {//'-'
                text: "删除",
                iconCls: "icon-remove",
                handler: function () {
                    //选中要删除的行
                    var rows = $("#dc").datagrid("getSelections");
                    if (rows.length > 0) {//选中几行的话触发事件
                        $.messager.confirm("提示", "您确定要删除这些数据吗？一旦删除，无法撤销。", function (res) {//提示是否删除
                            if (res) {
                                for (var i = 0; i < rows.length; i++) {
                                    delnum();
                                    var index = $("#dc").datagrid('getRowIndex', rows[i]);
                                    $("#dc").datagrid('deleteRow', index);
                                }
                            }
                        });
                    }
                }
            }]
      });
     
        var isRead = $("#OldDepart")[0].tagName.toLocaleLowerCase();
         if(isRead!="span"){
            //加载所属大区
            $("#OldDepart").combobox({
                url:"rule?wf_num=R_CRMVR_B141",
                valueField:'id',
                textField:'text',
                onLoadSuccess:function() {
                    groupdata = $('#OldDepart').combobox('getData');
                    if (groupdata && groupdata.length==1){
                        $("#OldDepart").combobox('select',groupdata[0].id);
                    }
                }
            });
        }
    }

    var WF_DocStatus = $("#WF_DocStatus").val();
    if(WF_DocStatus=="Draft"){
        var cCusCode = $("#cCusCode").text();
        fznext();//暂存时加载客户信息数据
    }
    
    //回退回来的表单客户信息不能修改
    var cCusName = $("#cCusName").val();
    if(cCusName!=""){
        $('#cCusCode').attr("disabled","disabled");
    	$('#Department').attr("disabled","disabled");
    	$('#cCusDefine6').attr("disabled","disabled");
    	$('#cCusFax').attr("disabled","disabled");
    	$('#cCusPhone').attr("disabled","disabled");
    	$('#cCusHand').attr("disabled","disabled");
    	$('#Cust_TYPE').attr("disabled","disabled");
    	$('#cCusDefine7').attr("disabled","disabled");
    	$('#cCusDefine2').attr("disabled","disabled");
    	//$('#OldDepart').attr("disabled","disabled");
    }
    
    
    //隐藏页面自带的BottomForm
    $("#BottomToolbar").hide();
    
    //在非客服环节隐藏"授权类型"字段
    if(currentNodeid=="T10002"||currentNodeid=="T10018"||currentNodeid=="T10004"||currentNodeid=="T10023"||currentNodeid=="T10019"||currentNodeid=="T10005"||currentNodeid=="T10006"){
        $("#license").hide();
        $("#SQLX").hide();
    }else{
        $("#license").show();
        $("#SQLX").show();
    }
    
}

//点击选择按钮调用方法
function SelInventory(){
    var a = $("#WF_DocUnid").val();
    var processid = $("#WF_Processid").val();
    var OldDepart = $("#OldDepart").combobox('getValue');
    if(OldDepart==""){
        alert("【请选择所属大区！】");
        return false;
    }
    var url="view?wf_num=V_CRMVR_G019&DocUnid="+$("#WF_DocUnid").val()+"&processid="+processid+"&OldDepart="+OldDepart;
    var swidth=screen.availWidth;
    var sheight=screen.availHeight;
    var wwidth=1000;
    var wheight=500;	
    var wleft=(swidth/2-0)-wwidth/2;
    var wtop=(sheight/2-0)-wheight/2;
    //新增部分----添加新的产品时，保存表中现有数据
    stopEditRow1();
    GetEditorValList();
    //新增结束
    window.open(url,'pwin','Width='+wwidth+'px,Height='+wheight+'px,Left='+wleft+',Top='+wtop+',status=no,resizable=yes,scrollbars=no,resezie=no');
}

//获取要删除的数据
function delnum(){
    var DocUnid = $("#WF_DocUnid").val();
    stopEditRow1();
    var rows = $("#dc").datagrid("getSelections");
	var val="";
	var v = "";
	var valList = "";
	//var fdList = "cInvCode";//编辑表格所有可编辑字段
	var fdList = "WF_OrUnid";
	var fdArray = fdList.split(","); //分隔数组分别取值
	$.each(rows, function (i, row) {
	    val="";
	    for(j=0;j<fdArray.length;j++){
	        v = eval("row."+fdArray[j]);
	        jQuery.ajax({
	            type:"post",
                async:false,
                url:"rule?wf_num=R_CRMVR_B139&DocUnid="+DocUnid+"&WF_OrUnid="+v+"",
                dataType:"text", 
	        });
	    }
	});
}


//停止编辑
function stopEditRow1(){
	$('#dc').datagrid('endEdit', editFlag);
	editFlag=undefined;
}

//插入新的行
function insertRow(){
    //插入新行
    stopEditRow1();
    var i=$('#dc').datagrid('getData').rows.length;
    var riseCol=i+1;
	$('#dc').datagrid('insertRow',{index:i,row:{}});   //,insertRow
}

//获取编辑表格所有值,并保存手动填写的数据
function GetEditorValList(){
    stopEditRow1();
    var rows = $("#dc").datagrid('getRows');
	var val="";
	var v = "";
	var valList = "";
	var fdList = "WF_OrUnid,cInvName,OrderNo,Define3";//编辑表格所有可编辑字段
	var fdArray = fdList.split(","); //分隔数组分别取值
	$.each(rows, function (i, row) {
	    val="";
	    for(j=0;j<fdArray.length;j++){
	        v = eval("row."+fdArray[j]);
	        val = val +"<"+fdArray[j]+">"+v+"</"+fdArray[j]+">";
	    }
	    
		//累加编辑表格值
		valList = valList+"<attrbute>"+val+"</attrbute>";
	});
	var xmlValList = "<editinfo>"+valList+"</editinfo>";
	$("#EditorValList").val(xmlValList);
	var DocUnid = $("#WF_DocUnid").val();
	var ProcessId = $("#WF_Processid").val();
	jQuery.ajax({
	  type:"post",
      async:false,
      //url:"rule?wf_num=R_CRMVR_B138&xmlValList="+xmlValList+"&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      //url:"rule?wf_num=R_CRMVR_B138&xmlValList="+encodeURIComponent(xmlValList)+"&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      url:"rule?wf_num=R_CRMVR_B138&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      dataType:"text",
      data:{xmlValList:xmlValList}
	});
}

function GetEditorValList1(){
    stopEditRow1();
    var rows = $("#dc").datagrid('getRows');
	var val="";
	var v = "";
	var valList = "";
	var fdList = "WF_OrUnid,Define4";//编辑表格所有可编辑字段
	var fdArray = fdList.split(","); //分隔数组分别取值
	$.each(rows, function (i, row) {
	    val="";
	    for(j=0;j<fdArray.length;j++){
	        v = eval("row."+fdArray[j]);
	        val = val +"<"+fdArray[j]+">"+v+"</"+fdArray[j]+">";
	    }
	    
		//累加编辑表格值
		valList = valList+"<attrbute>"+val+"</attrbute>";
	});
	var xmlValList = "<editinfo>"+valList+"</editinfo>";
	$("#EditorValList").val(xmlValList);
	var DocUnid = $("#WF_DocUnid").val();
	var ProcessId = $("#WF_Processid").val();
	jQuery.ajax({
	  type:"post",
      async:false,
      //url:"rule?wf_num=R_CRMVR_B138&xmlValList="+xmlValList+"&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      //url:"rule?wf_num=R_CRMVR_B146&xmlValList="+encodeURIComponent(xmlValList)+"&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      url:"rule?wf_num=R_CRMVR_B146&DocUnid="+DocUnid+"&ProcessId="+ProcessId+"",
      dataType:"text", 
      data:{xmlValList:xmlValList}
	});
}

function formonsubmit(){
    var currentNodeid = $("#WF_CurrentNodeid").val();
    if(currentNodeid=="T10002"){
        stopEditRow1();
        GetEditorValList();
        
        //授权公司名称为必填项
        var comCode = $("#comCode").val();
        //alert("comCode=========="+comCode);
        if(comCode=="0"){
            alert("【请选择【授权公司名称】！】");
            return false;
        }
        //判断授权客户+产品列表不能为空
        var docunid = $("#WF_DocUnid").val();
        var t;
        jQuery.ajax({
    	    type:"post",
    	    async:false,
    	    url:"rule?wf_num=R_CRMVR_B102&docunid="+docunid+"",
    	    dataType:"text",
    	    data:{},
    	    success:function(data){
    	      t=data;  
    	    }
    	});
    	if(t==0){
    	   alert("【请填写表体信息！】");
    	   return false;
    	}
    	
    	//验证产品和授权有效期不能为空
    	var a;
    	jQuery.ajax({
    	    type:"post",
    	    async:false,
    	    url:"rule?wf_num=R_CRMVR_B144&docunid="+docunid+"",
    	    dataType:"text",
    	    data:{},
    	    success:function(data){
    	      a=data;
    	    }
    	});
    	if(a==1){
    	    alert("【请填写【授权客户+产品列表】中的产品信息！】");
    	    return false;
    	}
    	jQuery.ajax({
    	    type:"post",
    	    async:false,
    	    url:"rule?wf_num=R_CRMVR_B145&docunid="+docunid+"",
    	    dataType:"text",
    	    data:{},
    	    success:function(data){
    	      a=data;  
    	    }
    	});
    	if(a==1){
    	    alert("【请填写【授权客户+产品列表】中的授权有效期！】");
    	    return false;
    	}
    }else if(currentNodeid=="T10014"||currentNodeid=="T10038"){
        stopEditRow1();
        GetEditorValList1();
    }
	//return false to exit submit
	$('#cCusCode').removeAttr("disabled");
	$('#comCode1').removeAttr("disabled");
	$('#Department').removeAttr("disabled");
	$('#cCusDefine6').removeAttr("disabled");
	$('#cCusFax').removeAttr("disabled");
	$('#cCusPhone').removeAttr("disabled");
	$('#cCusHand').removeAttr("disabled");
	$('#Cust_TYPE').removeAttr("disabled");
	$('#cCusDefine7').removeAttr("disabled");
	$('#cCusDefine2').removeAttr("disabled");
	//$('#OldDepart').removeAttr("disabled");
	$('#ccdefine5').removeAttr("disabled");
	$('#ccdefine6').removeAttr("disabled");
	$('#ccdefine7').removeAttr("disabled");
	
	
}
function SaveDocumentCallBack(btnAction,rs){
	//保存成功后的回调函数
	alert(rs.msg); 
	location.href="view?wf_num=V_S005_G001";
	try{opener.$('#dg').datagrid('reload');}catch(e){}
	window.close();
}


//模糊查询客户名称
function getCustomerName(val){
  changeCustomerName();
  var b;
  var cCusName = $("#cCusName").val();
  //模糊匹配客户名称
  jQuery.ajax({
      type:"post",
      async:false,
      url:"rule?wf_num=R_CRMVR_B140&cCusName="+cCusName+"",
      dataType:"text",
      data:{
         cCusName:$("#cCusName").text() 
      },
      success:function(data){
          checkword(data);
      }
  });
}

//点击页面任意位置取消搜索框
document.onclick = doc_click;
function doc_click(){
    document.getElementById("showmenu").style.display="none";
}

function changeCustomerName(){
    //变成可编辑状态
    //$('#cCusCode').removeAttr("disabled");
	$('#Department').removeAttr("disabled");
	$('#cCusDefine6').removeAttr("disabled");
	$('#cCusFax').removeAttr("disabled");
	$('#cCusPhone').removeAttr("disabled");
	$('#cCusHand').removeAttr("disabled");
	$('#Cust_TYPE').removeAttr("disabled");
	$('#cCusDefine7').removeAttr("disabled");
	$('#cCusDefine2').removeAttr("disabled");
	$('#ccdefine5').removeAttr("disabled");
	$('#ccdefine6').removeAttr("disabled");
	$('#ccdefine7').removeAttr("disabled");
	
	//情况当前字段中的内容
	$('#cCusCode').val("");
	$('#comCode1').val("");
	$('#Department').val("");
	$('#cCusDefine6').val("");
	$('#cCusFax').val("");
	$('#cCusPhone').val("");
	$('#cCusHand').val("");
	$('#Cust_TYPE').val("");
	$('#cCusDefine7').val("");
	$('#cCusDefine2').val("");
	$('#ccdefine5').val("");
	$('#ccdefine6').val("");
	$('#ccdefine7').val("");
}

//扩展模糊查询出来的序列值
function checkword(val){
    var wordvalue=document.getElementById("cCusName").value.toLowerCase();
    var alltxt=val;
    var inhtml="<table border='2'\" width ='300px'>";
    var isyou=0;
    var alltxtName = alltxt.split(",");
    for(i=0;i<alltxtName.length;i++){
        var alltxtNameNext = alltxtName[i].split("|");
        var name = alltxtNameNext[0];
        var value = alltxtNameNext[1];
        var comCode = alltxtNameNext[2];
        inhtml=inhtml+"<tr><td onclick=\"fz(this.innerHTML)\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\" style=\"font-weight:bold\">"
        +name+"<p style=\"display:none\">"+value+"</p>"+"<p>"+comCode+"</p>"+"<td/></tr>";
        isyou=1;
    }
    inhtml=inhtml+"</table>";
    if (isyou==1)
    {
       document.getElementById("showmenu").innerHTML=inhtml;
       document.getElementById("showmenu").style.display="";
    }
    else
    {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
    }
    if (wordvalue=="")
    {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
    }
}

//给相关联的字段赋值
function fz(val){
    var b = val.split("<");
    var c = b[1].split(">");
    var d = b[3].split(">");
    var e = d[1].split("[");
    var f = e[1].split("]");
    var a;
    //客户名称赋值
    $("#cCusName").val(b[0]);
    document.getElementById("showmenu").style.display="none";
    $("#cCusCode").val(c[1]);
    var cCusCode = $("#cCusCode").val();
    $("#cCusCode").attr("disabled","disabled");
    $("#comCode1").val(f[0]);
    var comCode = $("#comCode1").val();
    $("#comCode1").attr("disabled","disabled");
    //省份(Department)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B049&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
            Department:$("#Department").text()
        },
        success:function(data){
            a = data;
        }
    });
    $("#Department").val(a);
    $("#Department").attr("disabled","disabled");
    
    //客户类别(cCusDefine6)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B050&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
            cCusDefine6:$("#cCusDefine6").text()
        },
        success:function(data){
            a = data;
        }
    });
    $("#cCusDefine6").val(a);
    $("#cCusDefine6").attr("disabled","disabled");
    
    //传真号码(cCusFax)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B051&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
            cCusFax:$("#cCusFax").text()
        },
        success:function(data){
            a = data;
           
        }
    });
    $("#cCusFax").val(a);
    $("#cCusFax").attr("disabled","disabled");
    
    //电话(cCusPhone)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B052&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
          cCusPhone:$("#cCusPhone").text() 
        },
        success:function(data){
            a = data;
        }
    });
    $("#cCusPhone").val(a);
    $("#cCusPhone").attr("disabled","disabled");
    
    //手机(cCusHand)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B053&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
            cCusHand:$("#cCusHand").text()
        },
        success:function(data){
            a = data;
        }
    });
    $("#cCusHand").val(a);
    $("#cCusHand").attr("disabled","disabled");
    
    //客户类型(Cust_TYPE)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B135&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
          Cust_TYPE:$("#Cust_TYPE").text()  
        },
        success:function(data){
            a = data;
        }
    });
    $("#Cust_TYPE").val(a);
    $("#Cust_TYPE").attr("disabled","disabled");
    
    //开票类型(cCusDefine2)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B056&cCusCode="+cCusCode+"&comCode="+comCode+"",
        dataType:"text",
        data:{
          cCusDefine2:$("#cCusDefine2").text() 
        },
        success:function(data){
            a = data;
        }
    });
    $("#cCusDefine2").val(a);
    $("#cCusDefine2").attr("disabled","disabled");
    
    //合作情况(ccdefine7)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B149&cCusCode="+cCusCode+"",
        dataType:"text",
        data:{
        },
        success:function(data){
            a = data;
        }
    });
    $("#ccdefine7").val(a);
    $("#ccdefine7").attr("disabled","disabled");
}


//模糊查询被授权的医院
function getCustomerName1(val){
  var b;
  var Hospital = $("#Hospital").val();
  //模糊匹配客户名称
  jQuery.ajax({
      type:"post",
      async:false,
      url:"rule?wf_num=R_CRMVR_B109&cCusName="+Hospital+"",
      dataType:"text",
      data:{
         Hospital:$("#Hospital").text() 
      },
      success:function(data){
          checkword1(data);
      }
  });
}

function checkword1(val){
    var wordvalue=document.getElementById("Hospital").value.toLowerCase();
    var alltxt=val;
    var inhtml="<table border='2'\">";
    var isyou=0;
    var alltxtName = alltxt.split(",");
    for(i=0;i<alltxtName.length;i++){
        var alltxtNameNext = alltxtName[i].split("|");
        var name = alltxtNameNext[0];
        var value = alltxtNameNext[1];
        inhtml=inhtml+"<tr><td onclick=\"fz1(this.innerHTML)\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\" style=\"font-weight:bold\">"
        +name+"<p style=\"display:none\">"+value+"</p>"+"<td/></tr>";
        isyou=1;
    }
    inhtml=inhtml+"</table>";
    if (isyou==1)
    {
       document.getElementById("showmenu1").innerHTML=inhtml;
       document.getElementById("showmenu1").style.display="";
    }
    else
    {
       document.getElementById("showmenu1").innerHTML="";
       document.getElementById("showmenu1").style.display="none";
    }
    if (wordvalue=="")
    {
       document.getElementById("showmenu1").innerHTML="";
       document.getElementById("showmenu1").style.display="none";
    }
}

function fz1(val){
    var b = val.split("<");
    var c = b[1].split(">");
    var a;
    //客户名称赋值
    $("#Hospital").val(b[0]);
    document.getElementById("showmenu1").style.display="none";
    var cCusCode = c[1];
    // //省份(Department)
    // jQuery.ajax({
    //     type:"post",
    //     async:false,
    //     url:"rule?wf_num=R_CRMVR_B049&cCusCode="+cCusCode+"",
    //     dataType:"text",
    //     data:{
    //         Department:$("#Department").text()
    //     },
    //     success:function(data){
    //         a = data;
    //     }
    // });
    
    // //根据被授权医院名称返回其所对应的大区
    // var b ;
    // jQuery.ajax({
    //     type:"post",
    //     async:false,
    //     url:"rule?wf_num=R_CRMVR_B105&a="+a+"",
    //     dataType:"text",
    //     data:{
    //         OldDepart:$("#OldDepart").text()
    //     },
    //     success:function(data){
    //         b=data;
    //     }
    // });
    // $("#OldDepart").val(b);
}

function GoToNextNode1(){ 
    var WF_UserName;
    var DepartmentIDNew;
    DepartmentIDNew = $("#DepartmentID").val();
    //ajax返回选择部门的ID
    var newId;
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B091&DepartmentID="+DepartmentIDNew+"",
        dataType:"text",
        data:{},
        success:function(data){
           newId= data; 
        }
    });
    var NodeList=""; //获得选中的节点
    var UserList="";//办理用户
    var NodeArray="";
    var Nodeid="";
    var obj="";
    var NodeName="";
    var MaxUserNum="";
    var MinUserNum="";
    var userList="";
    var UserArray="";
    if(newId==DepartmentID){
        //alert("没有转换部门");
        //提交到下一环节
    	$('input[name="WF_NextNodeSelect"]:checked').each(function(){
    		if(NodeList==""){NodeList=$(this).val();}
    		else{NodeList+=","+$(this).val();}
    	});
    	if($('input[name="WF_NextNodeSelect"]').length>0){ //看是否可以选择环节
    		if(NodeList==""){alert("请选择一个环节再提交!");return false;}
    		//获得审批用户列表
    		NodeArray=NodeList.split(",");
    		for(var i=0;i<NodeArray.length;i++)
    		{
    			Nodeid=NodeArray[i];
    			obj=$("#WF_"+Nodeid);
    			if(obj.length>0)
    			{
    				NodeName=obj.attr("NodeName");
    				MaxUserNum=obj.attr("MaxUserNum")-0;
    				MinUserNum=obj.attr("MinUserNum")-0;
    				//userList对应的是主部门对应的审批人
    				userList=obj.val()+"";
    				UserArray=userList.split(",");
    				if(UserArray.length>MaxUserNum && MaxUserNum!=0){alert("环节("+NodeName+")超出最大可选参与人数!");return false;}
    				if((userList=="null" || userList=="") && MinUserNum!=0)
    				{
    					alert("环节("+NodeName+")没有指定任务处理人!");
    					return false;
    				}
    				if(UserArray.length<MinUserNum){alert("环节("+NodeName+")少于最小参与人数!"); return false;}
    				for(var j=0;j<UserArray.length;j++)
    				{
    					if(UserArray[j]!=""){
    						if(UserList==""){
    						    UserList=UserArray[j]+"$"+Nodeid;
    						}else{
    						    UserList+=","+UserArray[j]+"$"+Nodeid;
    						}
    					}
    				}
    			}
    		}
    		$("#WF_NextNodeid").val(NodeList);
    		$("#WF_NextUserList").val(UserList);
    	}
    }else{
        //alert("转换了部门");
        //提交到下一环节
    	$('input[name="WF_NextNodeSelect"]:checked').each(function(){
    		if(NodeList==""){NodeList=$(this).val();}
    		else{NodeList+=","+$(this).val();}
    	});
    	if($('input[name="WF_NextNodeSelect"]').length>0){ //看是否可以选择环节
    		if(NodeList==""){alert("请选择一个环节再提交!");return false;}
    		//获得审批用户列表
    		NodeArray=NodeList.split(",");
    		for(var i=0;i<NodeArray.length;i++)
    		{
    			Nodeid=NodeArray[i];
    			obj=$("#WF_"+Nodeid);
    			if(obj.length>0)
    			{
    				NodeName=obj.attr("NodeName");
    				MaxUserNum=obj.attr("MaxUserNum")-0;
    				MinUserNum=obj.attr("MinUserNum")-0;
    				//userList是选择兼职部门后对应的审批人
                    WF_UserName = $("#WF_UserName").val();//登录账户的userid
                    //获取兼职部门领导
                    jQuery.ajax({
                        type:"post",
                        async:false,
                        url:"rule?wf_num=R_CRMVR_B090&DepartmentID="+newId+"",
                        dataType:"text",
                        data:{
                            },
                        success:function(data){
                            userList = data;
                        }
                    });
    				//alert("userList========"+userList);
    				UserArray=userList.split(",");
    				if(UserArray.length>MaxUserNum && MaxUserNum!=0){alert("环节("+NodeName+")超出最大可选参与人数!");return false;}
    				if((userList=="null" || userList=="") && MinUserNum!=0)
    				{
    					alert("环节("+NodeName+")没有指定任务处理人!");
    					return false;
    				}
    				if(UserArray.length<MinUserNum){alert("环节("+NodeName+")少于最小参与人数!"); return false;}
    				for(var j=0;j<UserArray.length;j++)
    				{
    					if(UserArray[j]!=""){
    						if(userList==""){
    						    userList=UserArray[j]+"$"+Nodeid;
    						}else{
    						    userList+=","+UserArray[j]+"$"+Nodeid;
    						}
    					}
    				}
    			}
    		}
    		$("#WF_NextNodeid").val(NodeList);
    		$("#WF_NextUserList").val(userList);
    	}
    }
	SaveDocument("EndUserTask","您确认要提交本流程吗?");
}



//给客户信息相关联的字段再次赋值
function fznext(){
    var a;
    //客户名称赋值
    var cCusName = $("#cCusName").val();
    //客户编号
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B115&cCusName="+cCusName+"",
        dataType:"text",
        data:{
            Department:$("#Department").text()
        },
        success:function(data){
            a = data;
        }
    });
    $("#cCusCode").val(a);
    $("#cCusCode").attr("disabled","disabled");
    var cCusCode = $("#cCusCode").val();
    if(cCusCode!=""){
        //省份(Department)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B049&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
                Department:$("#Department").text()
            },
            success:function(data){
                a = data;
            }
        });
        $("#Department").val(a);
        $("#Department").attr("disabled","disabled");
        //document.getElementById("Department").readOnly="readonly";
        
        //客户类别(cCusDefine6)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B050&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
                cCusDefine6:$("#cCusDefine6").text()
            },
            success:function(data){
                a = data;
            }
        });
        $("#cCusDefine6").val(a);
        $("#cCusDefine6").attr("disabled","disabled");
        
        //传真号码(cCusFax)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B051&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
                cCusFax:$("#cCusFax").text()
            },
            success:function(data){
                a = data;
               
            }
        });
        $("#cCusFax").val(a);
        $("#cCusFax").attr("disabled","disabled");
        
        //电话(cCusPhone)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B052&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
              cCusPhone:$("#cCusPhone").text() 
            },
            success:function(data){
                a = data;
            }
        });
        $("#cCusPhone").val(a);
        $("#cCusPhone").attr("disabled","disabled");
        
        //手机(cCusHand)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B053&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
                cCusHand:$("#cCusHand").text()
            },
            success:function(data){
                a = data;
            }
        });
        $("#cCusHand").val(a);
        $("#cCusHand").attr("disabled","disabled");
        
        //客户类型(Cust_TYPE)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B054&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
              Cust_TYPE:$("#Cust_TYPE").text()  
            },
            success:function(data){
                a = data;
            }
        });
        $("#Cust_TYPE").val(a);
        $("#Cust_TYPE").attr("disabled","disabled");
        
        //开票类型(cCusDefine2)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B056&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
              cCusDefine2:$("#cCusDefine2").text() 
            },
            success:function(data){
                a = data;
            }
        });
        $("#cCusDefine2").val(a);
        $("#cCusDefine2").attr("disabled","disabled");
        
        //开票方式(Invoice_Way)
        jQuery.ajax({
            type:"post",
            async:false,
            url:"rule?wf_num=R_CRMVR_B057&cCusCode="+cCusCode+"",
            dataType:"text",
            data:{
                Invoice_Way:$("#Invoice_Way").text()
            },
            success:function(data){
                a = data;
            }
        });
        $("#Invoice_Way").val(a);
        $("#Invoice_Way").attr("disabled","disabled");
        
        //授权产品线(ccdefine5)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B147&cCusCode="+cCusCode+"",
        dataType:"text",
        data:{
        },
        success:function(data){
            a = data;
        }
    });
    $("#ccdefine5").val(a);
    $("#ccdefine5").attr("disabled","disabled");
    //授权地理区域(ccdefine6)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B148&cCusCode="+cCusCode+"",
        dataType:"text",
        data:{
        },
        success:function(data){
            a = data;
        }
    });
    $("#ccdefine6").val(a);
    $("#ccdefine6").attr("disabled","disabled");
    //合作情况(ccdefine7)
    jQuery.ajax({
        type:"post",
        async:false,
        url:"rule?wf_num=R_CRMVR_B149&cCusCode="+cCusCode+"",
        dataType:"text",
        data:{
        },
        success:function(data){
            a = data;
        }
    });
    $("#ccdefine7").val(a);
    $("#ccdefine7").attr("disabled","disabled");
        
        // var a = $("#Department").val();
        // //根据被授权医院名称返回其所对应的大区
        // var b ;
        // jQuery.ajax({
        //     type:"post",
        //     async:false,
        //     url:"rule?wf_num=R_CRMVR_B105&a="+a+"",
        //     dataType:"text",
        //     data:{
        //         OldDepart:$("#OldDepart").text()
        //     },
        //     success:function(data){
        //         b=data;
        //     }
        // });
        // $("#OldDepart").val(b);
        // $("#OldDepart").attr("disabled","disabled");
    }
    
}


//增加解除锁定表
var flag = true;
window.onbeforeunload = function () {
    if (flag) {
    var evt = window.event || arguments[0];
    var userAgent = navigator.userAgent;
        if (userAgent.indexOf("Firefox") > 0) {
             $.ajax({
                        url:'r?wf_num=R_finance_B030&WF_DocUnid='+$("#WF_DocUnid").val(),
                        type:'post',
                        dataType:'json',
                        contentType: "application/json; charset=utf-8",
                        async:false, 
                        success:function(data) {
                        }
                    });
        }
    }
}